
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using Apps.IDAL.RC;
using Apps.Models.RC;
using Apps.IBLL.RC;
namespace Apps.BLL.RC
{
	public partial class RC_WEIGHING_RECORDBLL: Virtual_RC_WEIGHING_RECORDBLL,IRC_WEIGHING_RECORDBLL
	{
        

	}
	public class Virtual_RC_WEIGHING_RECORDBLL
	{
        [Dependency]
        public IRC_WEIGHING_RECORDRepository m_Rep { get; set; }

        public List<V_RC_WEIGHING_RECORDModel> GetVWeighingRecordList(ref GridPager pager, Expression<Func<V_RC_WEIGHING_RECORD, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                var queryData = db.V_RC_WEIGHING_RECORD.Where(whereLambda);
                pager.totalRows = queryData.Count();
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                //return CreateModelList(ref queryData);
                return TransHelper.TransExpList<V_RC_WEIGHING_RECORD, V_RC_WEIGHING_RECORDModel>(queryData.ToList());
            }
        }

        public List<V_RC_WEIGHINGModel> GetVWeighingList(ref GridPager pager, Expression<Func<V_RC_WEIGHING, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                var queryData = db.V_RC_WEIGHING.Where(whereLambda);
                pager.totalRows = queryData.Count();
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                //return CreateModelList(ref queryData);
                return TransHelper.TransExpList<V_RC_WEIGHING, V_RC_WEIGHINGModel>(queryData.ToList());
            }
        }

        public V_RC_WEIGHING_RECORDModel GetVItem(Expression<Func<V_RC_WEIGHING_RECORD, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                var entity = db.V_RC_WEIGHING_RECORD.SingleOrDefault(whereLambda);
                return TransHelper.TransExp<V_RC_WEIGHING_RECORD, V_RC_WEIGHING_RECORDModel>(entity);
            }
        }

        public V_RC_WEIGHINGModel GetVWEIGHINGItem(Expression<Func<V_RC_WEIGHING, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                var entity = db.V_RC_WEIGHING.SingleOrDefault(whereLambda);
                return TransHelper.TransExp<V_RC_WEIGHING, V_RC_WEIGHINGModel>(entity);
            }
        }

        public virtual List<RC_WEIGHING_RECORDModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<RC_WEIGHING_RECORD> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>a.RECORD_ID.Contains(queryStr)
								|| a.PROCESS_ID.Contains(queryStr)
								|| a.WEIGHING_ID.Contains(queryStr) 
								|| a.GROSS_NODE_ID.Contains(queryStr)
								|| a.GROSS_USER_ID.Contains(queryStr) 
								//|| a.NET_NODE_ID.Contains(queryStr)
								//|| a.NET_USER_ID.Contains(queryStr) 
								|| a.RECORD_USER.Contains(queryStr) 
								|| a.TEMPLATE_ID.Contains(queryStr) 
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
		 
		public virtual List<RC_WEIGHING_RECORDModel> GetPagerList(ref GridPager pager, Expression<Func<RC_WEIGHING_RECORD, bool>> whereLambda )
        {

            IQueryable<RC_WEIGHING_RECORD> queryData = m_Rep.GetList(whereLambda); 
             
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<RC_WEIGHING_RECORDModel> GetPagerListByComb(ref GridPager pager, Expression<Func<RC_WEIGHING_RECORD, bool>> whereLambda )
        {

            IQueryable<RC_WEIGHING_RECORD> queryData = m_Rep.GetList(); 
             
			queryData = queryData.Where(whereLambda.Compile()).AsQueryable();

            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<RC_WEIGHING_RECORDModel> GetList(Expression<Func<RC_WEIGHING_RECORD, bool>> whereLambda )
        {

            IQueryable<RC_WEIGHING_RECORD> queryData = m_Rep.GetList(whereLambda); 
       
            return CreateModelList(ref queryData);
        }

	    public virtual List<RC_WEIGHING_RECORDModel> GetListByComb(Expression<Func<RC_WEIGHING_RECORD, bool>> whereLambda )
        {

            IQueryable<RC_WEIGHING_RECORD> queryData = m_Rep.GetList(); 

			queryData = queryData.Where(whereLambda.Compile()).AsQueryable();
       
            return CreateModelList(ref queryData);
        }

		public virtual List<RC_WEIGHING_RECORDModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<RC_WEIGHING_RECORDModel>();
		}

        public virtual List<RC_WEIGHING_RECORDModel> CreateModelList(ref IQueryable<RC_WEIGHING_RECORD> queryData)
        {

            List<RC_WEIGHING_RECORDModel> modelList = (from r in queryData
                                              select new RC_WEIGHING_RECORDModel
                                              {
													RECORD_ID = r.RECORD_ID,
													PROCESS_ID = r.PROCESS_ID,
													WEIGHING_ID = r.WEIGHING_ID,
													GROSS_MODE = r.GROSS_MODE,
													GROSS = r.GROSS,
													GROSS_NODE_ID = r.GROSS_NODE_ID,
													GROSS_USER_ID = r.GROSS_USER_ID,
													GROSS_TIME = r.GROSS_TIME,
													TARE = r.TARE,
													NET = r.NET,
													COEFFECENT = r.COEFFECENT,
													CUTBILL = r.CUTBILL,
													BALANCE = r.BALANCE,
                                                  TARE_MODE = r.TARE_MODE,
                                                  TARE_NODE_ID = r.TARE_NODE_ID,
                                                  TARE_USER_ID = r.TARE_USER_ID,
                                                  TARE_TIME = r.TARE_TIME,
                                                  RECORD_TYPE = r.RECORD_TYPE,
													RECORD_USER = r.RECORD_USER,
													RECORD_TIME = r.RECORD_TIME,
													TEMPLATE_ID = r.TEMPLATE_ID,
													APPROVAL_STATUS = r.APPROVAL_STATUS,
													RADOM = r.RADOM,
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, RC_WEIGHING_RECORDModel model)
        {
            try
            {
                RC_WEIGHING_RECORD entity = m_Rep.GetById(model.RECORD_ID);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new RC_WEIGHING_RECORD();
               				entity.RECORD_ID = model.RECORD_ID;
				entity.PROCESS_ID = model.PROCESS_ID;
				entity.WEIGHING_ID = model.WEIGHING_ID;
				entity.GROSS_MODE = model.GROSS_MODE;
				entity.GROSS = model.GROSS;
				entity.GROSS_NODE_ID = model.GROSS_NODE_ID;
				entity.GROSS_USER_ID = model.GROSS_USER_ID;
				entity.GROSS_TIME = model.GROSS_TIME;
				entity.TARE = model.TARE;
				entity.NET = model.NET;
				entity.COEFFECENT = model.COEFFECENT;
				entity.CUTBILL = model.CUTBILL;
				entity.BALANCE = model.BALANCE;
                entity.TARE_MODE = model.TARE_MODE;
                entity.TARE_NODE_ID = model.TARE_NODE_ID;
                entity.TARE_USER_ID = model.TARE_USER_ID;
                entity.TARE_TIME = model.TARE_TIME;
                entity.RECORD_TYPE = model.RECORD_TYPE;
				entity.RECORD_USER = model.RECORD_USER;
				entity.RECORD_TIME = model.RECORD_TIME;
				entity.TEMPLATE_ID = model.TEMPLATE_ID;
				entity.APPROVAL_STATUS = model.APPROVAL_STATUS;
				entity.RADOM = model.RADOM;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
		 public virtual bool Deletes(ref ValidationErrors errors,Expression<Func<RC_WEIGHING_RECORD, bool>> whereLambda)
         {
		        try
				{
					using (DBContainer db = new DBContainer())
					{
				
							foreach (var entity in db.RC_WEIGHING_RECORD.Where(whereLambda))
							{
								db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
							}

							if(db.SaveChanges()>0)
							{
								return true;
							}
							else
							{
								return false;
							}
			
					}
				}
				catch (Exception ex)
				{
					errors.Add(ex.Message);
					ExceptionHander.WriteException(ex);
					return false;
				}
        }

         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, RC_WEIGHING_RECORDModel model)
        {
            try
            {
                RC_WEIGHING_RECORD entity = m_Rep.GetById(model.RECORD_ID);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.RECORD_ID = model.RECORD_ID;
				entity.PROCESS_ID = model.PROCESS_ID;
				entity.WEIGHING_ID = model.WEIGHING_ID;
				entity.GROSS_MODE = model.GROSS_MODE;
				entity.GROSS = model.GROSS;
				entity.GROSS_NODE_ID = model.GROSS_NODE_ID;
				entity.GROSS_USER_ID = model.GROSS_USER_ID;
				entity.GROSS_TIME = model.GROSS_TIME;
				entity.TARE = model.TARE;
				entity.NET = model.NET;
				entity.COEFFECENT = model.COEFFECENT;
				entity.CUTBILL = model.CUTBILL;
				entity.BALANCE = model.BALANCE;
                entity.TARE_MODE = model.TARE_MODE;
                entity.TARE_NODE_ID = model.TARE_NODE_ID;
                entity.TARE_USER_ID = model.TARE_USER_ID;
                entity.TARE_TIME = model.TARE_TIME;
                entity.RECORD_TYPE = model.RECORD_TYPE;
				entity.RECORD_USER = model.RECORD_USER;
				entity.RECORD_TIME = model.RECORD_TIME;
				entity.TEMPLATE_ID = model.TEMPLATE_ID;
				entity.APPROVAL_STATUS = model.APPROVAL_STATUS;
				entity.RADOM = model.RADOM;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual RC_WEIGHING_RECORDModel GetById(object id)
        {
            if (IsExists(id))
            {
                RC_WEIGHING_RECORD entity = m_Rep.GetById(id);
                RC_WEIGHING_RECORDModel model = new RC_WEIGHING_RECORDModel();
                              				model.RECORD_ID = entity.RECORD_ID;
				model.PROCESS_ID = entity.PROCESS_ID;
				model.WEIGHING_ID = entity.WEIGHING_ID;
				model.GROSS_MODE = entity.GROSS_MODE;
				model.GROSS = entity.GROSS;
				model.GROSS_NODE_ID = entity.GROSS_NODE_ID;
				model.GROSS_USER_ID = entity.GROSS_USER_ID;
				model.GROSS_TIME = entity.GROSS_TIME;
				model.TARE = entity.TARE;
				model.NET = entity.NET;
				model.COEFFECENT = entity.COEFFECENT;
				model.CUTBILL = entity.CUTBILL;
				model.BALANCE = entity.BALANCE;
                model.TARE_MODE = entity.TARE_MODE;
                model.TARE_NODE_ID = entity.TARE_NODE_ID;
                model.TARE_USER_ID = entity.TARE_USER_ID;
                model.TARE_TIME = entity.TARE_TIME;
                model.RECORD_TYPE = entity.RECORD_TYPE;
				model.RECORD_USER = entity.RECORD_USER;
				model.RECORD_TIME = entity.RECORD_TIME;
				model.TEMPLATE_ID = entity.TEMPLATE_ID;
				model.APPROVAL_STATUS = entity.APPROVAL_STATUS;
				model.RADOM = entity.RADOM;
 
                return model;
            }
            else
            {
                return null;
            }
        }

		public virtual RC_WEIGHING_RECORDModel GetItem(Expression<Func<RC_WEIGHING_RECORD, bool>> whereLambda)
        {
 
                RC_WEIGHING_RECORD entity = m_Rep.GetItem(whereLambda);
                if (entity == null) return null;

                RC_WEIGHING_RECORDModel model = new RC_WEIGHING_RECORDModel();
                              				model.RECORD_ID = entity.RECORD_ID;
				model.PROCESS_ID = entity.PROCESS_ID;
				model.WEIGHING_ID = entity.WEIGHING_ID;
				model.GROSS_MODE = entity.GROSS_MODE;
				model.GROSS = entity.GROSS;
				model.GROSS_NODE_ID = entity.GROSS_NODE_ID;
				model.GROSS_USER_ID = entity.GROSS_USER_ID;
				model.GROSS_TIME = entity.GROSS_TIME;
				model.TARE = entity.TARE;
				model.NET = entity.NET;
				model.COEFFECENT = entity.COEFFECENT;
				model.CUTBILL = entity.CUTBILL;
				model.BALANCE = entity.BALANCE;
            model.TARE_MODE = entity.TARE_MODE;
            model.TARE_NODE_ID = entity.TARE_NODE_ID;
            model.TARE_USER_ID = entity.TARE_USER_ID;
            model.TARE_TIME = entity.TARE_TIME;
            model.RECORD_TYPE = entity.RECORD_TYPE;
				model.RECORD_USER = entity.RECORD_USER;
				model.RECORD_TIME = entity.RECORD_TIME;
				model.TEMPLATE_ID = entity.TEMPLATE_ID;
				model.APPROVAL_STATUS = entity.APPROVAL_STATUS;
				model.RADOM = entity.RADOM;
 
                return model; 
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<RC_WEIGHING_RECORDModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.RECORD_ID, "RECORD_ID");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.PROCESS_ID, "PROCESS_ID");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.WEIGHING_ID, "WEIGHING_ID");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.GROSS_MODE, "GROSS_MODE");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.GROSS, "GROSS");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.GROSS_NODE_ID, "GROSS_NODE_ID");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.GROSS_USER_ID, "GROSS_USER_ID");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.GROSS_TIME, "GROSS_TIME");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.TARE, "TARE");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.NET, "NET");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.COEFFECENT, "COEFFECENT");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.CUTBILL, "CUTBILL");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.BALANCE, "BALANCE");
            excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.TARE_MODE, "TARE_MODE");
            excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.TARE_NODE_ID, "TARE_NODE_ID");
            excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.TARE_USER_ID, "TARE_USER_ID");
            excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.TARE_TIME, "TARE_TIME");
            excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.RECORD_TYPE, "RECORD_TYPE");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.RECORD_USER, "RECORD_USER");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.RECORD_TIME, "RECORD_TIME");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.TEMPLATE_ID, "TEMPLATE_ID");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.APPROVAL_STATUS, "APPROVAL_STATUS");
				 excelFile.AddMapping<RC_WEIGHING_RECORDModel>(x => x.RADOM, "RADOM");
 
            //SheetName
            var excelContent = excelFile.Worksheet<RC_WEIGHING_RECORDModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new RC_WEIGHING_RECORDModel();
						 				  entity.RECORD_ID = row.RECORD_ID;
				  entity.PROCESS_ID = row.PROCESS_ID;
				  entity.WEIGHING_ID = row.WEIGHING_ID;
				  entity.GROSS_MODE = row.GROSS_MODE;
				  entity.GROSS = row.GROSS;
				  entity.GROSS_NODE_ID = row.GROSS_NODE_ID;
				  entity.GROSS_USER_ID = row.GROSS_USER_ID;
				  entity.GROSS_TIME = row.GROSS_TIME;
				  entity.TARE = row.TARE;
				  entity.NET = row.NET;
				  entity.COEFFECENT = row.COEFFECENT;
				  entity.CUTBILL = row.CUTBILL;
				  entity.BALANCE = row.BALANCE;
                entity.TARE_MODE = row.TARE_MODE;
                entity.TARE_NODE_ID = row.TARE_NODE_ID;
                entity.TARE_USER_ID = row.TARE_USER_ID;
                entity.TARE_TIME = row.TARE_TIME;
                entity.RECORD_TYPE = row.RECORD_TYPE;
				  entity.RECORD_USER = row.RECORD_USER;
				  entity.RECORD_TIME = row.RECORD_TIME;
				  entity.TEMPLATE_ID = row.TEMPLATE_ID;
				  entity.APPROVAL_STATUS = row.APPROVAL_STATUS;
				  entity.RADOM = row.RADOM;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<RC_WEIGHING_RECORDModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        RC_WEIGHING_RECORD entity = new RC_WEIGHING_RECORD();
                       						entity.RECORD_ID = model.RECORD_ID;
						entity.PROCESS_ID = model.PROCESS_ID;
						entity.WEIGHING_ID = model.WEIGHING_ID;
						entity.GROSS_MODE = model.GROSS_MODE;
						entity.GROSS = model.GROSS;
						entity.GROSS_NODE_ID = model.GROSS_NODE_ID;
						entity.GROSS_USER_ID = model.GROSS_USER_ID;
						entity.GROSS_TIME = model.GROSS_TIME;
						entity.TARE = model.TARE;
						entity.NET = model.NET;
						entity.COEFFECENT = model.COEFFECENT;
						entity.CUTBILL = model.CUTBILL;
						entity.BALANCE = model.BALANCE;
                        entity.TARE_MODE = model.TARE_MODE;
                        entity.TARE_NODE_ID = model.TARE_NODE_ID;
                        entity.TARE_USER_ID = model.TARE_USER_ID;
                        entity.TARE_TIME = model.TARE_TIME;
                        entity.RECORD_TYPE = model.RECORD_TYPE;
						entity.RECORD_USER = model.RECORD_USER;
						entity.RECORD_TIME = model.RECORD_TIME;
						entity.TEMPLATE_ID = model.TEMPLATE_ID;
						entity.APPROVAL_STATUS = model.APPROVAL_STATUS;
						entity.RADOM = model.RADOM;
 
                        db.RC_WEIGHING_RECORD.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
